import Vue from "vue";
import VueRouter from "vue-router";

import Home from "@/views/home";
import LegalReview from "@/views/legalReview";
import SearchCreditInfo from "@/views/searchCreditInfo";
import EnterpriseInfoQuery from "@/views/enterpriseInfoQuery";
import BusinessRole from "@/views/businessRole";
import TelecomLicenseDetails from "@/views/telecomLicenseDetails";
import CredentialDetails from "@/views/credentialDetails";
import OpinionsSuggestions from "@/views/opinionsSuggestions";
import legalPolicyLibrary from "@/views/legalPolicyLibrary";
import businessPermit from "@/views/businessPermit";
import particulars from "@/views/legalPolicyLibrary/particulars";
import businessParticulars from "@/views/businessPermit/businessParticulars";
import ExamineResult from "@/views/examineResult";
import error from "@/views/error/404.vue";
import search from "@/components/enterpriseDetails/ShortestPath/search";
//获取原型对象push函数
const originalPush = VueRouter.prototype.push;

//修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    meta: {
      keepAlive: true, //缓存
    },
  },
  {
    path: "/searchCreditInfo",
    name: "SearchCreditInfo",
    component: SearchCreditInfo,
    meta: {
      isEnterpriseInfo: true, //是否进入到企业信息页面
    },
  },
  {
    path: "/enterpriseInfoQuery",
    name: "EnterpriseInfoQuery",
    component: EnterpriseInfoQuery,
    meta: {
      isEnterSearchCredit: true, //是否进入到资信搜索页面
    },
  },
  {
    path: "/businessRole",
    name: "BusinessRole",
    component: BusinessRole,
    meta: {
      isEnterSearchCredit: true, //是否进入到资信搜索页面
    },
  },
  {
    path: "/telecomLicenseDetails",
    name: "TelecomLicenseDetails",
    component: TelecomLicenseDetails,
  },
  {
    path: "/credentialDetails",
    name: "CredentialDetails",
    component: CredentialDetails,
  },
  {
    path: "/opinionsSuggestions",
    name: "OpinionsSuggestions",
    component: OpinionsSuggestions,
  },
  {
    path: "/legalReview",
    name: "LegalReview",
    component: ()=>import("@/views/legalReview"),
    meta: {
      keepAlive: true, //缓存
    },
  },
  {
    path: "/legalPolicyLibrary",
    name: "legalPolicyLibrary",
    component: legalPolicyLibrary,
    meta: {
      keepAlive: false, //缓存
    },
  },
  {
    path: "/search",
    name: "search",
    component: search,
    meta: {
      keepAlive: false, //缓存
    },
  },
  {
    path: "/businessPermit",
    name: "businessPermit",
    component: businessPermit,
    meta: {
      keepAlive: false, //缓存
    },
  },
  {
    path: "/particulars",
    name: "particulars",
    component: particulars,
    meta: {
      keepAlive: false, //缓存
    },
  },
  {
    path: "/businessParticulars",
    name: "businessParticulars",
    component: businessParticulars,
    meta: {
      keepAlive: false, //缓存
    },
  },
  {
    path: "/examineResult",
    name: "ExamineResult",
    component: ExamineResult,
  },
  {
    path: "*",
    name: "404",
    component: error,
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.VUE_APP_PUBLIC_PATH,//这里必须和vue.config.js中配置的base保持一致
  routes,
});
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
});

export default router;
